<?PHP

namespace SDF\Lib\File;

/**
 * Class Thumbnail 缩略图处理类
 * @package SDF\Lib\File
 */
class Thumbnail {
    protected static $imageTypeArray = array(
        0 => 'UNKNOWN',
        1 => 'GIF',
        2 => 'JPEG',
        3 => 'PNG',
        4 => 'SWF',
        5 => 'PSD',
        6 => 'BMP',
        7 => 'TIFF_II',
        8 => 'TIFF_MM',
        9 => 'JPC',
        10 => 'JP2',
        11 => 'JPX',
        12 => 'JB2',
        13 => 'SWC',
        14 => 'IFF',
        15 => 'WBMP',
        16 => 'XBM',
        17 => 'ICO',
        18 => 'COUNT'
    );

    /**
     * 获取图片文件的相关信息，用于存数据库
     * @param string $imagePath
     * @param string $prefix
     * @return array
     * @throws \Exception
     */
    public static function getImagesFileInfo(string $imagePath,string $prefix = ''){
        if (!is_file($imagePath) or is_dir($imagePath)){
            throw new \Exception('早不到文件:'.$imagePath);
        }
        $images_info = @\getimagesize($imagePath);
        if($images_info === FALSE) return [];
        $images_document = array(
            'images_name' => \basename($imagePath),
            'file_size' => \filesize($imagePath),
            'width' => $images_info[0],
            'height'=> $images_info[1],
            'type' =>  self::$imageTypeArray[$images_info[2]],
            'mime' => $images_info['mime'],
            'url' => str_replace(DIRECTORY_SEPARATOR,'/','/'.\str_replace($prefix,'',$imagePath)),
        );
        return $images_document;
    }

    /**
     * 生成缩略图，并直接输出图片
     * @param string $imagePath 原图地址
     * @param string $seve_imagePath 缩略图保存地址
     * @param int $width 缩略图宽度
     * @param int $height 缩略图高度
     * @throws \Exception
     */
    public static function thumbnailImage(string $imagePath,string $seve_imagePath,int $width,int $height) {
        if(!is_dir(dirname($seve_imagePath))){
            throw new \Exception('目录不存在：'.dirname($seve_imagePath));
        }
        if(is_file($seve_imagePath)){
            throw new \Exception('缩略图已生成：'.$seve_imagePath);
        }
        if(!is_writable(dirname($seve_imagePath))){
            throw new \Exception('目录不可写：'.dirname($seve_imagePath));
        }
        if(!is_file($imagePath)){
            throw new \Exception('找不到文件:'.$imagePath);
        }
        if(!class_exists('Imagick',false)){
            throw new \Exception('你的PHP环境未安装Imagick扩展！');
        }
        $imagick = new \Imagick(realpath($imagePath));
        //$imagick->setbackgroundcolor('rgb(64, 64, 64)');//设置背景色
        //$imagick->thumbnailImage($width, $height, true, true);//调整图片大小
        $imagick->cropThumbnailImage($width, $height);//先缩放，再截切指定大小的图片
        $imagick->writeImage($seve_imagePath);//写入文件
        $filesize = filesize($seve_imagePath);
        $mime = $imagick->getImageMimeType();
        header("Content-Type: ".$mime);
        header("Accept-Ranges: bytes");
        header("Accept-Length:".$filesize);
        header("Content-Length:".$filesize);
        echo $imagick->getImageBlob();
        $imagick->destroy();
    }
}
